import { order, transport } from "../../api";
const debug = process.env.NODE_ENV !== "production";

const MUTATION_TYPES = {
  // 订单
  DEVICETRACE: "DEVICETRACE",
  CHANGEDEVICETRACE: "CHANGEDEVICETRACE",
  EXPIRATIONTIME: "EXPIRATIONTIME",
  // 运单
  WAYBILL_DEVICETRACE: "WAYBILL_DEVICETRACE",
  WAYBILL_CHANGEDEVICETRACE: "WAYBILL_CHANGEDEVICETRACE",
  WAYBILL_EXPIRATIONTIME: "WAYBILL_EXPIRATIONTIME",
};

const state = {
  deviceTraces: {},
  expirationTime: null,
  waybillDeviceTraces: {},
  waybillExpirationTime: null,
};

const getters = {
  deviceTracesObj (state) {
    return state.deviceTraces;
  },
  waybillDeviceTracesObj (state) {
    return state.waybillDeviceTraces;
  },
};

const mutations = {
  [MUTATION_TYPES.DEVICETRACE] (state, device) {
    const deviceTraces = { ...state.deviceTraces };
    deviceTraces[device.orderId] = device;
    state.deviceTraces = deviceTraces;
  },
  [MUTATION_TYPES.CHANGEDEVICETRACE] (state, devices) {
    state.deviceTraces = devices;
  },
  [MUTATION_TYPES.EXPIRATIONTIME] (state, time) {
    state.expirationTime = time;
  },

  [MUTATION_TYPES.WAYBILL_DEVICETRACE] (state, device) {
    const waybillDeviceTraces = { ...state.waybillDeviceTraces };
    waybillDeviceTraces[device.waybillId] = device;
    state.waybillDeviceTraces = waybillDeviceTraces;
  },
  [MUTATION_TYPES.WAYBILL_CHANGEDEVICETRACE] (state, devices) {
    state.waybillDeviceTraces = devices;
  },
  [MUTATION_TYPES.WAYBILL_EXPIRATIONTIME] (state, time) {
    state.waybillExpirationTime = time;
  },

};


class DeviceTraceLoader {
  #orderIds = []
  add (orderId) {
    const orderIdSet = new Set(this.#orderIds);
    orderIdSet.add(orderId);
    this.#orderIds = [...orderIdSet];
  }
  async orderDeviceTrace ({ commit, getters }, { }) {
    const orderId = this.#orderIds.pop();
    if (!orderId) {
      return
    }
    if (getters.deviceTracesObj[orderId]) {
      return
    }
    const response = await order.orderDeviceTrace({ orderIds: [orderId] });
    if (response?.code === 0) {
      if (response?.data && response?.data.length) {
        const device = { ...response.data[0], orderId }
        commit(MUTATION_TYPES.DEVICETRACE, device);
        return response.data
      }
    }
  }
  // async orderIdsDeviceTrace ({ commit, getters }, { orderIds }) {
  //   const { code, data } = await order.orderDeviceTrace({ orderIds });
  //   if (code === 0) {
  //     if (data && data.length) {
  //       const deviceTraces = { ...getters.deviceTracesObj };
  //       data.forEach(element => {
  //         deviceTraces[element.orderId] = element;
  //       });
  //       commit(MUTATION_TYPES.CHANGEDEVICETRACE, deviceTraces);
  //       return data
  //     }
  //   }
  // }
}

class WaybillDeviceTraceLoader {
  #waybillIds = []
  add (waybillId) {
    const waybillIdSet = new Set(this.#waybillIds);
    waybillIdSet.add(waybillId);
    this.#waybillIds = [...waybillIdSet];
  }
  async waybillDeviceTrace ({ commit, getters }, { }) {
    const waybillId = this.#waybillIds.pop();
    if (!waybillId) {
      return
    }
    if (getters.waybillDeviceTracesObj[waybillId]) {
      return
    }
    const response = await transport.waybillDeviceTrace(waybillId);
    if (response?.code === 0) {
      if (response?.data) {
        const device = { ...response.data, waybillId }
        commit(MUTATION_TYPES.WAYBILL_DEVICETRACE, device);
        return response.data
      }
    }
  }
}

const LOADER = new DeviceTraceLoader();
const WAYBILLLOADER = new WaybillDeviceTraceLoader();

const actions = {
  async orderDeviceTrace ({ commit, getters }, { orderId }) {
    LOADER.add(orderId)
    setTimeout(() => LOADER.orderDeviceTrace({ commit, getters }, {}), 300)
  },
  // async orderIdsDeviceTrace ({ commit, getters }, { orderIds }) {
  //   setTimeout(() => LOADER.orderIdsDeviceTrace({ commit, getters }, { orderIds }), 300)
  // },
  async waybillDeviceTrace ({ commit, getters }, { waybillId }) {
    WAYBILLLOADER.add(waybillId)
    setTimeout(() => WAYBILLLOADER.waybillDeviceTrace({ commit, getters }, {}), 300)
  },
  async cleanDeviceTrace ({ commit }, { data }) {
    commit(MUTATION_TYPES.CHANGEDEVICETRACE, data);
    commit(MUTATION_TYPES.EXPIRATIONTIME, null);
    commit(MUTATION_TYPES.WAYBILL_CHANGEDEVICETRACE, data);
    commit(MUTATION_TYPES.WAYBILL_EXPIRATIONTIME, null);
  }
};

export default {
  strict: debug,
  namespaced: true,
  state,
  getters,
  mutations,
  actions
};
